package onlinecalsys.client.view.gui;

/****************************************************************/
/*                      SaveAppointment	                            */
/*                                                              */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;

import onlinecalsys.client.view.View;

/**
 * Summary description for SaveAppointment
 * 
 */
public class SaveAppointment extends JFrame {
	// Variables declaration
	private JLabel jLabelSaveDate;
	private JLabel jLabelSaveAppointmentTime;
	private JLabel jLabelSaveAppointment;
	private JTextField jTextFieldSaveDate;
	private JTextField jTextFieldSaveAppointmentTime;
	private JTextArea jTextAreaAppointment;
	private JScrollPane jScrollPane2;
	private JButton jButtonSaveAppointment;
	private JPanel contentPaneAppointmentID;
	// End of variables declaration
	private View mainView;

	public SaveAppointment(View mainView) {
		super();
		this.mainView = mainView;
		initializeComponent();
		
		// load frame logo
		BufferedImage image = null;
		try {
			URL u = this.getClass().getResource(
			"../../../../");
			File f = new File(u.getPath().replaceAll("bin/", "images/middle1.png"));
			System.out.println(f.getAbsolutePath());
			image = ImageIO.read(f);
			
			
		} catch (IOException e) {
			new Error("failed loading middle1.png");
		} catch (Exception e) {
			new Error("failed loading middle1.png");
		}
		this.setIconImage(image);

		this.setVisible(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Windows Form Designer. Otherwise, retrieving design
	 * might not work properly. Tip: If you must revise this method, please
	 * backup this GUI file for JFrameBuilder to retrieve your design properly
	 * in future, before revising this method.
	 */
	private void initializeComponent() {
		jLabelSaveDate = new JLabel();
		jLabelSaveAppointmentTime = new JLabel();
		jLabelSaveAppointment = new JLabel();
		jTextFieldSaveDate = new JTextField();
		jTextFieldSaveAppointmentTime = new JTextField();
		jTextAreaAppointment = new JTextArea();
		jScrollPane2 = new JScrollPane();
		jButtonSaveAppointment = new JButton();
		contentPaneAppointmentID = (JPanel) this.getContentPane();
		
		jTextFieldSaveDate.setText("Date format: dd/MM/yyyy");

		// 
		// jLabelSaveDate
		// 
		jLabelSaveDate.setText("Date:");
		// 
		// jLabelSaveAppointmentTime
		// 
		jLabelSaveAppointmentTime.setText("Time:");
		// 
		// jLabelSaveAppointment
		// 
		jLabelSaveAppointment.setText("Appointment:");
		// 
		// jTextFieldSaveDate
		// 
		jTextFieldSaveDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jTextFieldSaveDate_actionPerformed(e);
			}

		});
		// 
		// jTextFieldSaveAppointmentTime
		// 
		jTextFieldSaveAppointmentTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jTextFieldSaveAppointmentTime_actionPerformed(e);
			}

		});
		// 
		// jTextAreaAppointment
		// 
		jTextAreaAppointment.setText("\n");
		// 
		// jScrollPane2
		// 
		jScrollPane2.setViewportView(jTextAreaAppointment);
		// 
		// jButtonSaveAppointment
		// 
		jButtonSaveAppointment.setText("Save");
		jButtonSaveAppointment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jButtonSaveAppointment_actionPerformed(e);
			}

		});
		// 
		// contentPaneAppointmentID
		// 
		contentPaneAppointmentID.setLayout(null);
		contentPaneAppointmentID.setBorder(new TitledBorder("Save"));
		addComponent(contentPaneAppointmentID, jLabelSaveDate, 75, 23, 60, 18);
		addComponent(contentPaneAppointmentID, jLabelSaveAppointmentTime, 75,
				48, 93, 18);
		addComponent(contentPaneAppointmentID, jLabelSaveAppointment, 36, 72,
				68, 18);
		addComponent(contentPaneAppointmentID, jTextFieldSaveDate, 104, 23,
				132, 22);
		addComponent(contentPaneAppointmentID, jTextFieldSaveAppointmentTime,
				104, 47, 134, 22);
		addComponent(contentPaneAppointmentID, jScrollPane2, 103, 75, 370, 132);
		addComponent(contentPaneAppointmentID, jButtonSaveAppointment, 401,
				232, 78, 28);
		// 
		// SaveAppointment
		// 
		this.setTitle("SaveAppointment");
		this.setLocation(new Point(79, 29));
		this.setSize(new Dimension(500, 300));
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setResizable(false);
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container, Component c, int x, int y,
			int width, int height) {
		c.setBounds(x, y, width, height);
		container.add(c);
	}

	// 
	// TODO: Add any appropriate code in the following Event Handling Methods
	// 
	private void jTextFieldSaveDate_actionPerformed(ActionEvent e) {
		System.out
				.println("\njTextFieldSaveDate_actionPerformed(ActionEvent e) called.");
		// TODO: Add any handling code here

	}

	private void jTextFieldSaveAppointmentTime_actionPerformed(ActionEvent e) {
		System.out
				.println("\njTextFieldSaveAppointmentTime_actionPerformed(ActionEvent e) called.");
		// TODO: Add any handling code here

	}

	private void jButtonSaveAppointment_actionPerformed(ActionEvent e) {

		if(mainView.newAppointment(jTextAreaAppointment.getText(),
				jTextFieldSaveDate.getText(), jTextFieldSaveAppointmentTime
						.getText())) {
			mainView.updateAppoinments(jTextFieldSaveDate.getText());
			this.dispose();
			
		}

	}

	// 
	// TODO: Add any method code to meet your needs in the following area
	// 

	// ============================= Testing ================================//
	// = =//
	// = The following main method is just for testing this class you built.=//
	// = After testing,you may simply delete it. =//
	// ======================================================================//
	// public static void main(String[] args)
	// {
	// JFrame.setDefaultLookAndFeelDecorated(true);
	// JDialog.setDefaultLookAndFeelDecorated(true);
	// try
	// {
	// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
	// }
	// catch (Exception ex)
	// {
	// System.out.println("Failed loading L&F: ");
	// System.out.println(ex);
	// }
	// new SaveAppointment();
	// }
	// = End of Testing =

}
